<?php
/**
 * APP 支付算签
 */
namespace App\Libraries;

use App\Models\Config;

include_once 'wechat_pay/lib/WxPay.Api.php';
include_once 'alipay_sdk/AopSdk.php';
class AppPay {
	
	/**
	 * @param $order_no
	 * @param $procut_name
	 * @param $price
	 * @param $notify_url
	 * @return array
	 * 微信支付
	 */
	public function wechat($order_no,$procut_name,$price,$notify_url){
	    if(!isset($price)){
	        print_r(json_error('金额参数错误'));
	        exit();
        }
		$input = new \WxPayUnifiedOrder();
		$input->SetBody($procut_name);
		$input->SetOut_trade_no($order_no);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetTotal_fee($price * 100);		// 分为单位，数据表是元为单位，故乘100
		$input->SetNotify_url($notify_url);
		$input->SetTrade_type("APP");

		$order = \WxPayApi::unifiedOrder($input);
		
		$param = [
			'appid'		=> $order['appid'],
			'partnerid'		=> $order['mch_id'],
			'prepayid'		=> $order['prepay_id'],
			'package'		=> 'Sign=WXPay',
			'noncestr'		=> \WxPayApi::getNonceStr(),
			'timestamp'		=> time(),
		];

		ksort($param);
		$param['key'] = \WxPayConfig::config('wechat_key');
		$param2 = [];
		foreach($param as $k => $v){
			array_push($param2,$k.'='.$v);
		}
		$sign = strtoupper(md5(implode('&',$param2)));
		$param['sign'] = $sign;
		return $param;
//		$jsApiParameters = $jsApi->GetJsApiParameters($order);
	}
	
	// 微信支付回调算签，成功返回true，失败返回false
	public function wechatCheckSign(){
		// return bool
		$error_msg = '';
		$status = \WxpayApi::notify(array($this, 'wechatCheckSignSuccess'), $error_msg);
		return $status;
	}
	// 原xml转数组后参数原样返回
	public function wechatCheckSignSuccess($post){
		return $post;
	}
	
	
	/**
	 * @param $order_no
	 * @param $procut_name
	 * @param $price
	 * @param $notify_url
	 * 支付宝支付
	 */
	public function alipay($order_no,$procut_name,$price,$notify_url){

		$aop = new \AopClient();
		$appid = Config::key('alipay_appid')->value('value');
		//$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
		$aop->appId = $appid;
//		$aop->rsaPrivateKey = '请填写开发者私钥去头去尾去回车，一行字符串';
		$aop->rsaPrivateKey = file_get_contents(__DIR__.'/alipay_sdk/pem/rsa_private_key.pem');
//		$aop->alipayrsaPublicKey='请填写支付宝公钥，一行字符串';
		$aop->alipayrsaPublicKey = file_get_contents(__DIR__.'/alipay_sdk/pem/alipay_public_key.pem');
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		$request = new \AlipayTradeAppPayRequest();
		$bizcontent = json_encode([
			'subject'			=> $procut_name,
			'out_trade_no'		=> $order_no,
			'total_amount'		=> $price,
			'product_code'		=> 'QUICK_MSECURITY_PAY',
		]);
		$request->setBizContent($bizcontent);
		$request->setNotifyUrl($notify_url);
		$result = $aop->sdkExecute($request);
		return $result;
	}
	
	// 支付宝支付算签
	public function alipayCheckSign(){
		$aop = new \AopClient();
		$aop->alipayrsaPublicKey = file_get_contents(__DIR__.'/alipay_sdk/pem/alipay_public_key.pem');
		$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
		return $flag;
	}

	public function bank_union($order_no,$procut_name,$price,$notify_url){
	
	}
}